package casa.depesos.app.com.frag;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import casa.depesos.app.com.activity.BaseInfoActivity;
import casa.depesos.app.com.activity.CardSureActivity;
import casa.depesos.app.com.activity.CheckActivity;
import casa.depesos.app.com.activity.MainActivity;
import casa.depesos.app.com.activity.OkActivity;
import casa.depesos.app.com.databinding.FragmentNetFailBinding;
import casa.depesos.app.com.utils.Utils;


public class NetFailFragment extends BaseFragment {

    FragmentNetFailBinding binding;
    String title;
    boolean showTop = true;

    public NetFailFragment setTitle(String s) {
        return setTitle(s, true);
    }

    public NetFailFragment setTitle(String s, boolean showTop) {
        title = s;
        this.showTop = showTop;
        return this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentNetFailBinding.inflate(inflater, container, false);
        if (showTop) {
            binding.top.top.setVisibility(View.VISIBLE);
            binding.top.top.setPadding(0, Utils.getStatusBarHeight(getActivity()), 0, 0);
        } else {
            binding.top.top.setVisibility(View.GONE);
        }
        if (getActivity() instanceof MainActivity) {
            binding.top.back.setVisibility(View.INVISIBLE);
            binding.top.title.setText("");
        } else if (!TextUtils.isEmpty(title)) {
            binding.top.title.setText(title);
            binding.top.back.setOnClickListener(view -> getActivity().finish());
        }

        binding.refresh.setOnClickListener(view -> {
            refresh();
        });

        return binding.getRoot();
    }

    private void refresh() {
        if (getActivity() instanceof MainActivity) {
            ((MainActivity) getActivity()).getData();
        } else if (getActivity() instanceof BaseInfoActivity) {
            ((BaseInfoActivity) getActivity()).getData();
        } else if (getActivity() instanceof CardSureActivity) {
            ((CardSureActivity) getActivity()).getData();
        } else if (getActivity() instanceof CheckActivity) {
            ((CheckActivity) getActivity()).getData(true);
        } else if (getActivity() instanceof OkActivity) {
            ((OkActivity) getActivity()).getData();
        } else if (getParentFragment() instanceof OrderFragment) {
            ((OrderFragment) getParentFragment()).getData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
